package android.got.tasks;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;

import android.accounts.Account;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.Preference;

public class Utils extends Preference {

    private static final String PREF            = "GoTasks2710";
    public static final String PREF_ACCOUNT     = "accountName";
    public static final String PREF_TASKLIST    = "taskListID";
    public static final String PREF_USEACC      = "use_account";

    public Utils(Context context) {
        super(context);
    }

    public static void writeToPref(final Context context, final String key, final String value) {
        SharedPreferences settings = context.getSharedPreferences(PREF, 0);
        SharedPreferences.Editor editor = settings.edit();

        editor.putString(key, value);
        editor.commit();
    }

    public static String getNormalizedDate(String date) {
        Pattern pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
        Matcher m = pattern.matcher(date);
        if (m.find()) {
           return m.group(3) + "." + m.group(2) + "." + m.group(1);
        }
        return date;
    }

    public static CharSequence[] getAvailableAccounts(final Context context) {
        GoogleAccountManager accountManager = new GoogleAccountManager(context);
        final Account[] accounts = accountManager.getAccounts();

        final int size = accounts.length;
        String[] names = new String[size];

        for (int i = 0; i < size; i++) {
            names[i] = accounts[i].name;
        }

        return names;
    }
}
